<template>
  <div style="height: 100%">
    <div class="top-bar">
      <div class="right">
        <el-button type="primary" @click="uploadFnJar()">上传</el-button>
      </div>
    </div>
    <el-table
        :data="fnList"
        stripe
        height="calc(100% - 28px)"
        style="width: 100%">
      <el-table-column
          label="序号"
          type="index"
          width="50">
      </el-table-column>
      <el-table-column
          label="文件名称"
          prop="wjmc">
      </el-table-column>
      <el-table-column
          label="方法列表"
          prop="wjmc">
        <template slot-scope="{row}">
          {{ getFnListStr(row.fnList) }}
        </template>
      </el-table-column>
      <el-table-column
          label="创建时间"
          sortable
          prop="cjsj"
          width="150px">
      </el-table-column>
      <el-table-column
          label="操作"
          align="center"
          width="330px">
        <template slot-scope="{row}">
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import commonMixins from "@/mixins/commonMixins";
import XjDialog from "@/components/XjDialog";

export default {
  name: "Xmgl",
  components: {XjDialog},
  mixins: [commonMixins],
  data() {
    return {
      fnList: []
    }
  },
  mounted() {
    this.queryFnList()
  },
  methods: {
    getFnListStr(fnList) {
      let newList = fnList.map((item, idx) => {
        let zwmc = item.zwmc
        let name = item.name
        return `${idx + 1}、${zwmc === name ? zwmc : `${zwmc}(${name})`}`
      })
      return newList.join(",")
    },
    async queryFnList() {
      this.fnList = await this.$api.queryFnList()
    },
    async uploadFnJar() {
      try {
        return await new Promise((resolve, reject) => {
          let timer = setTimeout(() => {
            return reject(new Error("选取文件超时"))
          }, 60 * 1000)
          let input = document.createElement("input")
          input.type = "file"
          input.accept = "application/java-archive"
          input.onchange = async (e) => {
            if (timer) {
              clearTimeout(timer)
            }
            let file = input.files[0]
            if (!file.name.endsWith(".jar")) {
              return reject(new Error("选择的文件不是jar包，请检查"))
            }
            let formData = new FormData()
            formData.append("file", file)
            let res = await this.$api.uploadFn(formData)
            this.$message({message: '上传成功', type: "success"})
            this.queryFnList()
            resolve()
          }
          input.click()
        })
      } catch (e) {
        this.$message({message: e.message, type: "error"})
      }
    }
  }
}
</script>
